package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.ChargeRecordVo;
import com.net.xpay.admin.vo.InvoiceVo;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.domain.invoice.Invoice;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.common.manager.invoice.InvoiceManager;
import com.net.xpay.common.service.InvoiceService;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.model.InvoiceSearchModel;
import com.net.xpay.core.model.OpenInvoiceModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.shard.ShardUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台发票管理
 86188
 */
@RestController
@RequestMapping("/admin/invoice-administration")
public class AdminInvoiceAdministrationController {

    @Autowired
    private InvoiceManager invoiceManager;

    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private UserShardManager userShardManager;

    @ApiOperation("搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'invoice-administration::search')")
    public ResultPagedList<InvoiceVo> searchInvoice(@RequestBody InvoiceSearchModel searchModel) {
        List<Invoice> records = invoiceManager.search(searchModel);
        List<InvoiceVo> vos = BeanCopierUtil.batchCopy(Invoice.class, InvoiceVo.class, records);
        long count = invoiceManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("查询关联的消费记录")
    @PostMapping("/list-related-charge-record")
    public ResultList<ChargeRecordVo> listRelatedChargeRecord(Long id) {
        List<ChargeRecord> list = invoiceService.adminListRelatedChargeRecord(id);
        List<ChargeRecordVo> vos = BeanCopierUtil.batchCopy(ChargeRecord.class, ChargeRecordVo.class, list);

        return new ResultList<>(vos);
    }

    @ApiOperation("开票")
    @PostMapping("/open-invoice")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'invoice-administration::open-invoice')")
    public ResultData<Boolean> openInvoice(@RequestBody OpenInvoiceModel model) {
        Preconditions.checkArgument(model.getUserId() != null);
        UserShard userShard = userShardManager.getById(model.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            invoiceService.openInvoice(model);
        } finally {
            ShardUtil.cleanShardContext();
        }
        return new ResultData<>(true);
    }

    @ApiOperation("开票失败")
    @PostMapping("/open-invoice-fail")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'invoice-administration::open-invoice-fail')")
    public ResultData<Boolean> openInvoiceFail(@RequestBody OpenInvoiceModel model) {
        UserShard userShard = userShardManager.getById(model.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            invoiceService.openInvoiceFail(model);
        } finally {
            ShardUtil.cleanShardContext();
        }

        return new ResultData<>(true);
    }
}
